/*
 ***************************************************************************************
 * EP for web developers.Supported By Junxworks
 * @Title:  FinanceServiceImpl.java   
 * @Package io.github.junxworks.qt.modules.tushare.service.impl   
 * @Description: (用一句话描述该文件做什么)   
 * @author: Administrator
 * @date:   2021年9月12日 下午3:58:30   
 * @version V1.0 
 * @Copyright: 2021 Junxworks. All rights reserved. 
 * 注意：
 *  ---------------------------------------------------------------------------------- 
 * 文件修改记录
 *     文件版本：         修改人：             修改原因：
 ***************************************************************************************
 */
package io.github.junxworks.qt.modules.tushare.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

import io.github.junxworks.junx.core.util.StringUtils;
import io.github.junxworks.qt.modules.data.service.DataStoreService;
import io.github.junxworks.qt.modules.tushare.Fields;
import io.github.junxworks.qt.modules.tushare.TushareAPIs;
import io.github.junxworks.qt.modules.tushare.entity.DStockBalancesheetOds;
import io.github.junxworks.qt.modules.tushare.entity.DStockCashflowOds;
import io.github.junxworks.qt.modules.tushare.entity.DStockFinaIndicatorOds;
import io.github.junxworks.qt.modules.tushare.entity.DStockFinaMainbz;
import io.github.junxworks.qt.modules.tushare.entity.DStockForecast;
import io.github.junxworks.qt.modules.tushare.entity.DStockHoldernumber;
import io.github.junxworks.qt.modules.tushare.entity.DStockIncomeOds;
import io.github.junxworks.qt.modules.tushare.mapper.FinanceMapper;
import io.github.junxworks.qt.modules.tushare.service.TushareService;

@Service("TushareFinanceService")
public class TushareFinanceService extends TushareService {

	@Autowired
	private DataStoreService dataStoreService;

	@Autowired
	private FinanceMapper financeMapper;

	/**
	 * 存储利润数据，period是财报季度的最后一天
	 *
	 * @param period 2020-12-31
	 */
	public void fetchIncomeData(String period) {
		handleDataByLastFinanceReportPeriod(period, d -> {
			_fetchIncomeData(d);
		});
	}

	private void _fetchIncomeData(String period) {
		Map<String, String> params = Maps.newHashMap();
		String pe = StringUtils.replace(period, "-", "");
		params.put("period", pe);
		consumeTushareDataStockByStock(TushareAPIs.FINA_INCOME, Fields.F_INCOME, DStockIncomeOds.class, params, l -> {
			if (!l.isEmpty()) {
				String tsCode = extractTsCode(l);
				financeMapper.deleteIncomeOdsByPeriod(tsCode, pe);
				dataStoreService.insertBatch(l);
			}
		});
		cleanIncomeData(period);
	}

	public void cleanIncomeData(String period) {
		financeMapper.deleteIncomeByPeriod(period);
		financeMapper.cleanIncomeByPeriod(period);
	}

	public void cleanIncomeDataByRange(String periodRange) {
		handleDataByLastFinanceReportPeriodRange(periodRange, period -> {
			cleanIncomeData(period);
		});
	}

	/**
	 * 抓取现金流量数据，period是财报季度的最后一天
	 *
	 * @param period 2020-12-31
	 */
	public void fetchCashflowData(String date) {
		handleDataByLastFinanceReportPeriod(date, d -> {
			_fetchCashflowData(d);
		});
	}

	private void _fetchCashflowData(String period) {
		Map<String, String> params = Maps.newHashMap();
		String pe = StringUtils.replace(period, "-", "");
		params.put("period", pe);
		consumeTushareDataStockByStock(TushareAPIs.FINA_CASHFLOW, Fields.F_CASHFLOW, DStockCashflowOds.class, params, l -> {
			if (!l.isEmpty()) {
				String tsCode = extractTsCode(l);
				financeMapper.deleteCashflowOdsByPeriod(tsCode, pe);
				dataStoreService.insertBatch(l);
			}
		});
		cleanCashflowData(period);
	}

	public void cleanCashflowData(String period) {
		financeMapper.deleteCashflowByPeriod(period);
		financeMapper.cleanCashflowByPeriod(period);
	}

	public void cleanCashflowDataByRange(String periodRange) {
		handleDataByLastFinanceReportPeriodRange(periodRange, period -> {
			cleanCashflowData(period);
		});
	}

	/**
	 * 存储资产负债，period是财报季度的最后一天
	 *
	 * @param period 2020-12-31
	 */
	public void fetchBalancesheetData(String date) {
		handleDataByLastFinanceReportPeriod(date, d -> {
			_fetchBalancesheetData(d);
		});
	}

	private void _fetchBalancesheetData(String period) {
		Map<String, String> params = Maps.newHashMap();
		String pe = StringUtils.replace(period, "-", "");
		params.put("period", pe);
		consumeTushareDataStockByStock(TushareAPIs.FINA_BALANCESHEET, Fields.F_BALANCESHEET, DStockBalancesheetOds.class, params, l -> {
			if (!l.isEmpty()) {
				String tsCode = extractTsCode(l);
				financeMapper.deleteBalancesheetOdsByPeriod(tsCode, pe);
				dataStoreService.insertBatch(l);
			}
		});
		cleanBalancesheetData(period);
	}

	public void cleanBalancesheetData(String period) {
		financeMapper.deleteBalancesheetByPeriod(period);
		financeMapper.cleanBalancesheetByPeriod(period);
	}

	public void cleanBalancesheetDataByRange(String periodRange) {
		handleDataByLastFinanceReportPeriodRange(periodRange, period -> {
			cleanBalancesheetData(period);
		});
	}

	/**
	 * 存储财务指标，period是财报季度的最后一天
	 *
	 * @param period 2020-12-31
	 */
	public void fetchFinaIndicatorData(String date) {
		handleDataByLastFinanceReportPeriod(date, d -> {
			_fetchFinaIndicatorData(d);
		});
	}

	private void _fetchFinaIndicatorData(String period) {
		Map<String, String> params = Maps.newHashMap();
		String pe = StringUtils.replace(period, "-", "");
		params.put("period", pe);
		consumeTushareDataStockByStock(TushareAPIs.FINA_INDICATOR, Fields.F_FINA_INDICATOR, DStockFinaIndicatorOds.class, params, l -> {
			if (!l.isEmpty()) {
				String tsCode = extractTsCode(l);
				financeMapper.deleteFinaIndicatorOdsByPeriod(tsCode, pe);
				dataStoreService.insertBatch(l);
			}
		});
		cleanFinaIndicatorData(period);
	}

	public void cleanFinaIndicatorData(String period) {
		financeMapper.deleteFinaIndicatorByPeriod(period);
		financeMapper.cleanFinaIndicatorByPeriod(period);
	}

	public void cleanFinaIndicatorDataByRange(String periodRange) {
		handleDataByLastFinanceReportPeriodRange(periodRange, period -> {
			cleanFinaIndicatorData(period);
		});
	}

	/**
	 * 存储主营业务，period是财报季度的最后一天
	 *
	 * @param period 2020-12-31
	 */
	public void fetchFinaMainBzData(String date) {
		handleDataByLastFinanceReportPeriod(date, d -> {
			_fetchFinaMainBzData(d);
		});
	}

	private void _fetchFinaMainBzData(String period) {
		Map<String, String> params = Maps.newHashMap();
		String pe = StringUtils.replace(period, "-", "");
		params.put("period", pe);
		consumeTushareDataStockByStock(TushareAPIs.FINA_MAINBZ, Fields.F_FINA_MAINBZ, DStockFinaMainbz.class, params, l -> {
			if (!l.isEmpty()) {
				String tsCode = extractTsCode(l);
				financeMapper.deleteFinaMainBzByCodeAndPeriod(tsCode, pe);
				dataStoreService.insertBatch(l);
			}
		});
	}

	/**
	* 存储业绩预报，period是财报季度的最后一天
	*
	* @param period 2020-12-31
	*/
	public void fetchForecastData(String period) {
		handleDataByLastFinanceReportPeriod(period, d -> {
			_fetchForecastData(d);
		});
	}

	private void _fetchForecastData(String period) {
		Map<String, String> params = Maps.newHashMap();
		params.put("period", period);
		consumeTushareDataStockByStock(TushareAPIs.FINA_FORECAST, Fields.F_FORECAST, DStockForecast.class, params, l -> {
			if (!l.isEmpty()) {
				String tsCode = extractTsCode(l);
				financeMapper.deleteForecastByPeriod(tsCode, period);
				dataStoreService.insertBatch(l);
			}
		});
	}

	/**
	 * 根据指定报告期来存储业绩预告
	 *
	 * @param periodRange the period range
	 */
	public void fetchForecastDataByRange(String periodRange) {
		handleDataByLastFinanceReportPeriodRange(periodRange, period -> {
			_fetchForecastData(period);
		});
	}

	/**
	 * 获取股东人数
	 *
	 * @param date the date
	 */
	public void fetchStockHolderNumber(String date) {
		handleDataByLastFinanceReportPeriod(date, period -> {
			_fetchfetchStockHolderNumber(period);
		});
	}

	public void fetchStockHolderCountByRange(String periodRange) {
		handleDataByLastFinanceReportPeriodRange(periodRange, period -> {
			_fetchfetchStockHolderNumber(period);
		});
	}

	private void _fetchfetchStockHolderNumber(String period) {
		Map<String, String> params = Maps.newHashMap();
		params.put("enddate", period);
		consumeTushareData(TushareAPIs.BASE_HOLDER_NUMBER, Fields.F_HOLDERS, DStockHoldernumber.class, params, l -> {
			if (!l.isEmpty()) {
				financeMapper.deleteStockHolderByDate(period);
				dataStoreService.insertBatch(l);
			}
		});
	}

}
